<?php
namespace app\single\controller;
use think\Db;
/**
 * #用户相关 {关于用户管理相关}
 */
class Wcollege extends Common{
    private $artcate_id=14; //微商学院分类ID

    function setting($isJson=true){
        $uid=$this->user['id'];
        $user=model('member')->read(['id'=>$uid],'id,vip_expire,coin,coin_expire');
        $vip_level=model('member')->isVip($uid);
        $_coin=config('setting.article_coin');
        $nums2=(int)config('setting.article_nums'); //每日可发产品数量
        $has=model('article')->where([
            'uid'=>$user['id'],
            's_time'=>['s_time','>=',strtotime(date('Y-m-d'))]
        ])->count();
        $nums=$nums2-$has; //该用户今日可发产品数量
        if($nums<0){
            $nums=0;
        }
        $isFirst=model('article')->where(['uid'=>$user['id']])->count();
        if($vip_level){
            if($isFirst){
                $_coin=config('setting.vip_article_coin');
            }else{
                $_coin=config('setting.vip_first_article_coin');
            }
        }
        
        
        $res=[
            'vip_level'=>$vip_level,
            'money'=>$_coin,
            'coin'=>$user['coin_expire']>time()?99999:$user['coin'],
            'coin_expire'=>$user['coin_expire'],

            'nums'=>$nums, //该用户今日可发产品数量
            'nums2'=>$nums2, //每日最多可发产品数量
            'count'=>$isFirst, //已发产品数量
            // 最大可发
            'max'=>$vip_level+config('setting.article_counts')+config('setting.article_vip_counts_inc'),
        ];
        if($isJson){
            return json('',0,$res);
        }
        return $res;
    }

    /**
     * 获取微商学院的二级分类
     * @return [type] [description]
     */
    function getCates($isJson=true){
        // 获取微商学院的二级分类
        $cate =model('artcate')->readAll([
            'pid'=>$this->artcate_id,
            'status'=>1,
        ],0,[
            'field'=>'id,name,icon'
        ])->toArray();
        $ids=[];
        $prefix=request()->domain().'/tagsicon/';
        foreach($cate as $k => $v) {
            if(!empty($v['icon'])){
                $cate[$k]['icon']=$prefix.$v['icon'];
            }
            $ids[]=$v['id'];
        }
        array_unshift($cate,['id'=>0,'name'=>'全部']);
        if($isJson){
            return json('',0,$cate);
        }
        return [$cate,$ids];
    }
    /**
     * 查询微商学院列表
     * @return json
     */
    function index(){
        $cate=$this->getCates(false);
        $data = [
            'cate'=>$cate[0]
        ];
        $map=[
            'status'=>1,
            'cate_id'=>['cate_id','in',$cate[1]],
        ];
        $options=[
            'order'=>'s_time desc',
            // 'with'=>['author','cate','praise','images'],
            // 'field'=>'id,uid,cate_id,wxId,tel,title,cover,content,s_time,browse,praise,belittle'
            'field'=>'id,cover,title,s_time'
        ];

        $list=model('article')->readAll($map,null,$options)->toArray();
        // var_dump($list);
        $hot=[];
        $domain=request()->domain();
        foreach ($list as $key => $value) {
            $value['article_path']=url('index/Single/helpDetail',[
                'id'=>$value['id'],
                'showBack'=>0,
            ],'html',$domain);

            // if(!empty($value['head_img'])){
            //     $value['head_img']=images($value['head_img']);
            // }
            if(!empty($value['cover'])){
                $value['cover']=images($value['cover']);
            }
            // foreach ($value['images'] as $k => $v) {
            //     $value['images'][$k]['link']=images($v['link']);
            // }
            $hot[]=$value;
        }
        // 整合数据
        $data['list'] = $hot;
        return json('',0,$data);
    }

    /**
     * 查询产品列表
     * @param  integer $cate_id 微商学院二级分类ID
     * @param  integer $mine    0：获取所有 1：获取我发布的
     * @return json
     */
    function product($cate_id=0,$mine=0){
        $map=[
            'status'=>1,
        ];
        if($cate_id>0){
            $has=model('artcate')->where(['id'=>$cate_id,'pid'=>$this->artcate_id])->count();
            if($has){
                $map['cate_id']=$cate_id;
            }
        }else{
            $cate=$this->getCates(false);
            $map['cate_id']=['cate_id','in',$cate[1]];
        }
        if($mine>0){
            $map['uid']=$this->user['id'];
        }
        
        $options=[
            'order'=>'s_time desc',
            //'with'=>['author','cate','praise','images'],
            //'field'=>'id,uid,cate_id,wxId,tel,cover,title,content,s_time,browse,praise,belittle'
            'field'=>'id,cover,title,s_time'
        ];

        $list=model('article')->readAll($map,0,$options)->toArray();
        $res=[];
        $domain=request()->domain();
        foreach ($list as $key => $value) {
            $value['article_path']=url('index/Single/helpDetail',[
                'id'=>$value['id'],
                'showBack'=>0,
            ],'html',$domain);
            // if(!empty($value['head_img'])){
            //     $value['head_img']=images($value['head_img']);
            // }
            if(!empty($value['cover'])){
                $value['cover']=images($value['cover']);
            }
            // foreach ($value['images'] as $k => $v) {
            //     $value['images'][$k]['link']=images($v['link']);
            // }
            $res[]=$value;
        }
        
        return json('',0,$res);
    }
    /**
     * 查询微商学院产品详情
     * @param  integer $aid 文章ID
     * @return [type]      [description]
     */
    function info($id=0){
        $map=[
            'id'=>$id,
            'status'=>1,
        ];
        $options=[
            'with'=>['author','cate','images'],
        ];
        $field='id,uid,wxId,tel,title,cover,s_time,cate_id,content,browse,praise,belittle';
        
        $info=model('article')->read($map,$field,$options);
        if(isset($info['images'])){
            if(!empty($info['head_img'])){
                $info['head_img']=images($info['head_img']);
            }
            if(!empty($info['cover'])){
                $info['cover']=images($info['cover']);
            }
            foreach ($info['images'] as $k => $v) {
                $info['images'][$k]['link']=images($v['link']);
            }
            // 增加浏览量
            $this->clicks($id);
        }
        if(isset($info['id'])){
            $info['is_parise']=null;
            $parise=model('praise')->read(['product_id'=>$info['id']],'convert(type,char(1)) as type');
            if(isset($parise)){
                $info['is_parise']=$parise['type'];
            }
        }
        return json('',0,$info);
    }

    /**
     * 文章点赞、贬低
     * @param  integer $id   文章ID
     * @param  integer $type 0：浏览（查询文章详情时调用）、1：点赞 2：贬低
     * @return json
     */
    function clicks($id=0,$type=0){
        $map=[
            'id'=>$id,
        ];
        if($type==0){ //浏览量
            model('article')->where($map)->setInc('browse');
            return json('');
        }elseif($type==1){ //点赞
            $uid=$this->user['id'];
            $has=model('praise')->where(['uid'=>$uid,'product_id'=>$id])->count();
            if($has){
                return json('您已经点过赞了',1);
            }
            $e=model('article')->where($map)->setInc('praise');
            if($e){
                model('praise')->add([
                    'uid'=>$uid,
                    'product_id'=>$id,
                    'type'=>1,
                ]);
            }
            return json('点赞成功');
        }elseif($type==2){ //贬低
            $uid=$this->user['id'];
            $has=model('praise')->where(['uid'=>$uid,'product_id'=>$id])->count();
            if($has){
                return json('您已经点过赞了',1);
            }
            $e=model('article')->where($map)->setInc('belittle');
            if($e){
                model('praise')->add([
                    'uid'=>$uid,
                    'product_id'=>$id,
                    'type'=>2,
                ]);
            }
            return json('谢谢点评');
        }
        return json('');
        
    }
}